<script setup name="Resorder">
import { listResorder, getResorder, delResorder, addResorder, updateResorder } from "@/api/system/resorder"
import userUserStore from "@/store/modules/user.js"
const userStore = userUserStore()
console.log(userStore)

const userId = userStore.id
console.log(userId)
const { proxy } = getCurrentInstance()
const { sys_order_status } = proxy.useDict('sys_order_status')

const resorderList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    resName: null,
    nickName: null,
    packageName: null,
    status: null,
    userId: userId,
  },
  rules: {
    resName: [
      { required: true, message: "餐厅名不能为空", trigger: "blur" }
    ],
    nickName: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    packageName: [
      { required: true, message: "套餐名不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询餐馆订单列表 */
function getList() {
  loading.value = true
  listResorder(queryParams.value).then(response => {
    resorderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    resName: null,
    nickName: null,
    packageName: null,
    price: null,
    ticketCount: null,
    validPeriod: null,
    status: null,
    orderTime: null,
    orderId: null,
    resId: null,
    customerId: null,
    packageId: null,
    userId: userId
  }
  proxy.resetForm("resorderRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.orderId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加餐馆订单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _orderId = row.orderId || ids.value
  getResorder(_orderId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改餐馆订单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["resorderRef"].validate(valid => {
    if (valid) {
      if (form.value.orderId != null) {
        updateResorder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addResorder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _orderIds = row.orderId || ids.value
  proxy.$modal.confirm('是否确认删除餐馆订单编号为"' + _orderIds + '"的数据项？').then(function() {
    return delResorder(_orderIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/resorder/export', {
    ...queryParams.value
  }, `resorder_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="餐厅名" prop="resName">
        <el-input
          v-model="queryParams.resName"
          placeholder="请输入餐厅名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套餐名" prop="packageName">
        <el-input
          v-model="queryParams.packageName"
          placeholder="请输入套餐名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable style="width: 200px">
          <el-option
            v-for="dict in sys_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:resorder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:resorder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="100px">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="餐厅名" align="center" prop="resName" />
<!--      设置用户名长度，超出部分用省略号表示-->
      <el-table-column label="用户名" align="center" prop="nickName">
        <template #default="scope">
          <el-tooltip placement="top" effect="light">
            <template #content>
              {{ scope.row.nickName }}
            </template>
            <span style="max-width: 100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
              {{ scope.row.nickName }}
            </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="套餐名" align="center" prop="packageName" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="购票数量" align="center" prop="ticketCount" />
      <el-table-column label="有效期" align="center" prop="validPeriod" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:resorder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改餐馆订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="resorderRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>


